package cn.lzwei.bilimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.lzwei.bilimall.ware.vo.MergePurchaseVo;
import cn.lzwei.bilimall.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.lzwei.bilimall.ware.entity.PurchaseEntity;
import cn.lzwei.bilimall.ware.service.PurchaseService;
import cn.lzwei.common.utils.PageUtils;
import cn.lzwei.common.utils.R;

import javax.validation.Valid;


/**
 * 采购信息
 *
 * @author lizewei
 * @email Lizewei2105@163.com
 * @date 2022-12-22 10:30:40
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 模拟完成采购：更新采购需求的状态 ——> 更新采购单状态 ——> 添加库存
     */
    @PostMapping("/done")
    public R done(@Valid @RequestBody PurchaseDoneVo purchaseDoneVo){
        purchaseService.done(purchaseDoneVo);
        return R.ok();
    }
    /**
     * 模拟领取采购单：需要验证采购单、采购需求的状态
     */
    @PostMapping("/received")
    public R receivedPurchase(@RequestBody List<Long> ids){
        purchaseService.receivedPurchase(ids);
        return R.ok();
    }
    /**
     * 合并采购需求：1.指定采购单，更新对应的采购需求以及采购单信息；2.无指定采购单，创建采购单并更新采购需求以及采购单信息
     */
    @PostMapping("/merge")
    public R mergePurchase(@RequestBody MergePurchaseVo mergePurchaseVo){
        purchaseService.mergePurchase(mergePurchaseVo);
        return R.ok();
    }
    /**
     * 获取采购单列表：状态为 新建、已分配
     */
    @GetMapping("/unreceive/list")
    public R unrecivePurchase(@RequestParam Map<String, Object> params){
        PageUtils page=purchaseService.getUnrecivePurchase(params);
        return R.ok().put("page",page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase){
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
		purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase){
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
